package com.springcloud.auth.utils;

import com.springcloud.commom.utils.SpringContextUtils;
import org.springframework.stereotype.Component;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import javax.annotation.Resource;
import java.io.*;
import java.util.Set;

@Component
public class JedisUtil {

    @Resource
    private JedisPool jedisPool;

    public void set(byte[] key, byte[] value) {
        Jedis jedis = getResource();
        try {
            jedis.set(key, value);
        } finally {
            jedis.close();
        }
    }

    public Set<String> getKeys(String key){
        Jedis jedis = getResource();
        try {
            Set<String> set = jedis.keys(key);

            return set;
        } finally {
            jedis.close();
        }
    }

    public String getSet(String key,String value){
        Jedis jedis = getResource();
        try {
            String result = jedis.getSet(key,value);

            return result;
        } finally {
            jedis.close();
        }
    }

    public int setNx(String key,String value){
        Jedis jedis = getResource();
        try {
            int result = jedis.setnx(key,value).intValue();
            return result;
        } finally {
            jedis.close();
        }
    }

    public String get(String key) {
        Jedis jedis = getResource();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }

    public void expire(byte[] key, int time) {
        Jedis jedis = getResource();
        try {
            jedis.expire(key, time);
        } finally {
            jedis.close();
        }
    }

    private Jedis getResource() {
        return jedisPool.getResource();
    }

    public byte[] get(byte[] key) {
        Jedis jedis = getResource();
        try {
            return jedis.get(key);
        } finally {
            jedis.close();
        }
    }

    public void del(byte[] key) {
        Jedis jedis = getResource();
        try {
            jedis.del(key);
        } finally {
            jedis.close();
        }
    }

    public void del(byte[] key,Integer db) {
        Jedis jedis = getResource();
        try {
            jedis.select(db);
            jedis.del(key);
        } finally {
            jedis.close();
        }
    }

    public void set(String key, Object obj, Integer expire,Integer sldb) {
        Jedis jedis = getResource();
        try {
			jedis.select(sldb);
            byte[] bytes = serialize(obj);
            jedis.set(key.getBytes(), bytes);

            jedis.expire(key, expire);
        } finally {
            jedis.close();
        }
    }

    public Object get(String key,Integer sldb) {
        Jedis jedis = getResource();
        try {
			jedis.select(sldb);
            byte[] bytes = jedis.get(key.getBytes());
            if(bytes == null){
                return null;
            }
            Object obj = unserizlize(bytes);
            return obj;
        } finally {
            jedis.close();
        }
    }

    private static byte[] serialize(Object object) {
        ObjectOutputStream objectOutputStream = null;
        ByteArrayOutputStream byteArrayOutputStream = null;
        try {
            byteArrayOutputStream = new ByteArrayOutputStream();
            objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
            objectOutputStream.writeObject(object);
            byte[] getByte = byteArrayOutputStream.toByteArray();
            return getByte;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    private static Object unserizlize(byte[] binaryByte) {
        ObjectInputStream objectInputStream = null;
        ByteArrayInputStream byteArrayInputStream = null;
        byteArrayInputStream = new ByteArrayInputStream(binaryByte);
        try {
            objectInputStream = new ObjectInputStream(byteArrayInputStream);
            Object obj = objectInputStream.readObject();
            return obj;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
